package com.wyj.wuliwuli.service.impl;

import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.common.PageParam;
import com.wyj.wuliwuli.entity.WuliUserVideoLike;
import com.wyj.wuliwuli.entity.WuliVideo;
import com.wyj.wuliwuli.entity.WuliVideoComment;
import com.wyj.wuliwuli.entity.mapper.WuliVideoCommentMapper;
import com.wyj.wuliwuli.service.WuliVideoCommentService;
import com.wyj.wuliwuli.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.pqc.math.linearalgebra.IntUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class WuliVideoCommentServiceImpl implements WuliVideoCommentService {

    private final WuliVideoCommentMapper wuliVideoCommentMapper;

    /**
     * 分页查询
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult page(AjaxRequest<PageParam> ajaxRequest) {
        PageParam pageParam = ajaxRequest.getData();
        if (pageParam == null) {
            pageParam = new PageParam();
        }
        Pageable pageable = PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), pageParam.getProperty());
        Page<WuliVideoComment> page = wuliVideoCommentMapper.findAll(pageable);
        return AjaxResult.ok(page);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult getById(Long id) {

        if (id == null) {
            return AjaxResult.fail("id不能为空");
        }
        Optional<WuliVideoComment> videocommentoptional = wuliVideoCommentMapper.findById(id);
        if (!videocommentoptional.isPresent()) {
            return AjaxResult.fail("该id的评论不存在或已被删除");
        }
        return AjaxResult.ok(videocommentoptional.get());
    }

    /**
     * 保存
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult save(AjaxRequest<WuliVideoComment> ajaxRequest) {
        WuliVideoComment videoComment = ajaxRequest.getData();
        String checkmsg = saveCheck(videoComment);
        if (!StringUtils.isBlank(checkmsg)) {
            return AjaxResult.fail(checkmsg);
        }
        wuliVideoCommentMapper.save(videoComment);
        return AjaxResult.of();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult deleteById(Long id) {
        if (id == null) {
            return AjaxResult.fail("id不能为空");
        }
        if (wuliVideoCommentMapper.existsById(id)) {
            wuliVideoCommentMapper.deleteById(id);
        }else {
            return AjaxResult.fail("id不存在或已被删除");
        }
        return AjaxResult.of();
    }

    /**
     * 根据视频id查询评论
     *
     * @param video_id
     * @return
     */
    @Override
    public AjaxResult getByVideoId(Long video_id) {
        if (video_id == null) {
            return AjaxResult.fail("视频id不能为空");
        }

        List<WuliVideoComment> list = wuliVideoCommentMapper.findAllByVideo_Id(video_id);

        return AjaxResult.ok(list);
    }

    public String saveCheck(WuliVideoComment wuliVideoComment) {
        if (wuliVideoComment == null) {
            return "请求数据不能为空";
        }
        if (wuliVideoComment.getVideo() == null) {
            return "发布评论的视频不能为空";
        }
        if (wuliVideoComment.getUser() == null) {
            return "发布评论的用户不能为空";
        }
        if (StringUtils.isBlank(wuliVideoComment.getMsg())) {
            return "发布的评论信息不能为空";
        }
        return null;
    }


}
